import datetime
import uuid
from src.util_config import *
# from bson import json_util, ObjectId

def get_date():
    create_time = datetime.datetime.now()
    return str(create_time)[0:19]

def add_timestamp(dict_data, flag="UPDATE"):
    now = datetime.datetime.now().strftime(TIME_FORMAT_TIME_ZONE)
    if flag == "UPDATE":
        dict_data[TIMESTAMP_FLAG_UPDATE] = now
    elif flag == "CREATE":
        dict_data[TIMESTAMP_FLAG_CREATE] = now
        dict_data[TIMESTAMP_FLAG_UPDATE] = now
    else:
        print(flag)
    return dict_data


def gen_uuid():
    return uuid.uuid4()

def set_end_date(end_date):
    schema_end_date = ""
    try:
        end_date_obj = datetime.datetime.strptime(end_date[0:10],"%Y-%m-%d")
        if end_date_obj.month <3 :
            schema_end_date = str(end_date_obj.year) + "-03" + "-31"
        elif end_date_obj.month < 6:
            schema_end_date = str(end_date_obj.year) + "-06" + "-30"
        elif end_date_obj.month < 9:
            schema_end_date = str(end_date_obj.year) + "-09" + "-30"
        else:
            schema_end_date = str(end_date_obj.year) + "-12" + "-31"
    except Exception as e:
        schema_end_date = "2019-01-01"
        return schema_end_date
        print("Error when parse date string : " + end_date)
    return schema_end_date

def set_start_date(start_date):
    schema_start_date = ""
    try:
        start_date_obj = datetime.datetime.strptime(start_date[0:10],"%Y-%m-%d")
        if start_date_obj.month <3 :
            schema_start_date = str(start_date_obj.year) + "-01" + "-01"
        elif start_date_obj.month < 6:
            schema_start_date = str(start_date_obj.year) + "-04" + "-01"
        elif start_date_obj.month < 9:
            schema_start_date = str(start_date_obj.year) + "-07" + "-01"
        else:
            schema_start_date = str(start_date_obj.year) + "-10" + "-01"
    except Exception as e:
        schema_start_date = "2019-01-01"
        return schema_start_date
        print("Error when parse date string : " + start_date)
    return schema_start_date

def is_leap_year(year):
    return  (year % 4 == 0 and year % 100 != 0) or year % 400 == 0

def add_lag_month(date):
    month = date.month
    year = date.year
    lag_date = ""
    if (month + MONTH_LAG) > 12:
        year = year + 1
        month = month + MONTH_LAG - 12
    if (month + MONTH_LAG) <= 12:
        if month + MONTH_LAG < 10:
            lag_date = str(year) + "-0" + str(month + MONTH_LAG) + "-01"
        else:
            lag_date = str(year) + "-" + str(month + MONTH_LAG) + "-01"
    return lag_date

def define_ecart_date(stat_start_date, stat_end_date):
    date_ecart_list = [str(stat_start_date)]
    start_date = datetime.datetime.strptime(stat_start_date[0:10],"%Y-%m-%d")
    end_date = datetime.datetime.strptime(stat_end_date[0:10],"%Y-%m-%d")
    while start_date < end_date:
        lag_date = add_lag_month(start_date)
        date_ecart_list.append(lag_date)
        start_date = datetime.datetime.strptime(lag_date[0:10],"%Y-%m-%d")

    return date_ecart_list
